package com.example.controller;


import com.example.entity.Orderform;
import com.example.service.IOrderformService;
import com.example.util.Code;
import com.example.util.Result;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tyhxzy
 * @since 2023-11-03
 */
@RestController
@RequestMapping("/s/orderform")
@CrossOrigin
public class OrderformController {

    @Autowired
    private IOrderformService iOrderformService;

    //商户查看订单
    @GetMapping("/findstatu/{id}")
    public Result findOrderformByStatus(@PathVariable Integer id,@RequestParam(required = false) Integer status){
        List<Map> orderformByStatus = iOrderformService.findOrderformByStatus(id,status);

        if (orderformByStatus!=null){
            return new Result(Code.GET_OK,orderformByStatus,"查询成功");
        }else {
            return new Result(Code.GET_ERR,"查询失败");
        }
    }

    //司机查看订单
    @GetMapping("/findnames/{id}")
    public Result findorderformByName(@PathVariable Integer id,@RequestParam(required = false) Integer status){
        List<Map> orderformByName = iOrderformService.findOrderformByName(id,status);
        if (orderformByName!=null){
            return new Result(Code.GET_OK,orderformByName,"查询成功");
        }else {
            return new Result(Code.GET_ERR,"查询失败");
        }
    }

    //商家根据状态查
    @GetMapping("/findstatus/{id}")
    public Result findOrderformStatusById(@PathVariable Integer id){
        Map orderformStatusById = iOrderformService.findOrderformStatusById(id);
        if (orderformStatusById!=null){
            return new Result(Code.GET_OK,orderformStatusById,"查询成功");
        }else {
            return new Result(Code.GET_ERR,"查询失败");
        }
    }

    //司机根据状态查
    @GetMapping("/findname/{id}")
    public Result findOrderformNameById(@PathVariable Integer id){
        Map orderformNameById = iOrderformService.findOrderformNameById(id);
        if (orderformNameById!=null){
            return new Result(Code.GET_OK,orderformNameById,"查询成功");
        }else {
            return new Result(Code.GET_ERR,"查询失败");
        }
    }

    /**
     * 司机下单成功后新增订单
     * @param orderform
     * @return
     */
    @PostMapping("/add")
    public Result insert(@RequestBody Orderform orderform){
        int insert = iOrderformService.insert(orderform);
        if(insert>0){
            return new Result(Code.SAVE_OK,insert,"新增成功");
        }else{
            return new Result(Code.SAVE_ERR,"新增失败");
        }
    }
    @GetMapping("/findorder/{id}")
    public Result findOrder(@PathVariable Integer id){
        Map<Object, Object> m = new HashMap<>();
        int sum = iOrderformService.findSum(id);
        Double price = iOrderformService.findPrice(id);
        m.put("sum",sum);
        m.put("price",price);
        return new Result(Code.SAVE_OK,m,"查询成功");
    }

    @GetMapping("/writercode/{writercode}")
    public Result findOrderform(@PathVariable String writercode){
        Orderform orderformwritercodr = iOrderformService.findOrderform(writercode);
        if (orderformwritercodr!=null && orderformwritercodr.getWritercodr()!=""){
            return new Result(Code.GET_OK,orderformwritercodr,"核销成功");
        }else {
            return new Result(Code.GET_ERR,"核销失败");
        }
    }

    @GetMapping("/writercodr/{zid}")
    public Result updatewritercoder(@PathVariable Integer zid){
        int i = iOrderformService.updateWritercoder(zid);
        if (i>0){
            return new Result(Code.UPDATE_OK,zid,"完成");
        }else {
            return new Result(Code.UPDATE_ERR,"失败");
        }
    }
}
